package com.jiruan.invoicemanagementsystem.dao.Imp;

import com.jiruan.invoicemanagementsystem.dao.IEmailDao;
import com.jiruan.invoicemanagementsystem.pojo.Email;
import com.jiruan.invoicemanagementsystem.pojo.Order;
import com.jiruan.invoicemanagementsystem.util.DBHelper;
import com.jiruan.invoicemanagementsystem.util.PageHelper;

import java.util.ArrayList;

public class IEmailDaoImp implements IEmailDao {


    //获取默认的邮箱信息
    @Override
    public Email selectDefaultEmail() {
        String sql = "select * from email where defaultStatus = 'A'";
        return (Email) DBHelper.currencySqlQuery(Email.class,sql).getFirst();
    }

    //根据条件将对应邮箱信息设置为默认,其他设为B
    @Override
    public int updateDefaultEmail(Email email) {
        String sql = "update email set defaultStatus = case when id = ? then 'A' else 'B' end";
        return DBHelper.currencySqlUpdate(sql,email.getId());
    }

//根据id设置邮箱信息
    public int updateEmailById(Email email) {
        String sql="update email set emailDetail=? where id=?";
        return DBHelper.currencySqlUpdate(sql,email.getEmailDetail(),email.getId());
    }

    //新添加一条邮箱信息  双联表
    @Override
    public int insertNewEmail(String emailDetail) {
           String sql= "insert into email (enterpriseId,emailDetail,defaultStatus) " +
                   "values ((select IFNULL(MAX(enterpriseId),0)+1 " +
                   "from (select enterpriseId from email) as temp2),?,'B')";
       Object[] obj=new Object[]{emailDetail};
       return DBHelper.currencySqlUpdate(sql,obj);
    }

    //根据id修改email的邮箱信息


    //获得所有邮箱信息
    @Override
    public ArrayList<Email> selectAllEmail() {
        String sql ="select *from email";


//        System.out.println(index);
        return DBHelper.currencySqlQuery(Email.class, sql);
    }

    public ArrayList<Email> selectPageAllEmail(PageHelper ph){
        String sql ="select *from email limit ?,?";

        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
//        System.out.println(index);
        return DBHelper.currencySqlQuery(Email.class, sql,index, ph.getShowNumber());
    }

    //获得所有邮箱条数
    public int queryEmailCount(){
        String sql ="select count(*) from email";
        return DBHelper.currencySqlQuery(sql);
    }



}
